/*
所有关于食物的代码都写在这里
 */

(function (w) {
    var list = [];
    var Food = function (width,height,bgColor,x,y,br) {
        this.width = width || 20;
        this.height = height || 20;
        this.bgColor = bgColor || 'green';
        this.x = x || 0;
        this.y = y || 0;
        this.borderRadius = br || 10;

    };

    Food.prototype.render = function (map) {
        remove(map);
        this.x = Math.floor(Math.random()*map.offsetWidth/this.width)*this.width;
        this.y = Math.floor(Math.random()*map.offsetHeight/this.height)*this.height;
        var div1 = document.createElement('div');
        div1.style.position = 'absolute';
        div1.style.left = this.x + 'px';
        div1.style.top = this.y + 'px';
        div1.style.backgroundColor = this.bgColor;
        div1.style.width = this.width+'px';
        div1.style.height = this.height+'px';
        div1.style.borderRadius = this.borderRadius + 'px';
        map.appendChild(div1);
        list.push(div1);
    };

    //删掉食物的方法
    function remove(map){
        for (var i = 0; i < list.length ; i++) {
            map.removeChild(list[i]);
        };
        //清空数组
        list = [];
    }

    w.Food = Food
}(window));